gusucode.com > VC++ MFC绘图类实战笔记源代码-源码程序 > VC++ MFC绘图类实战笔记源代码-源码程序/code/20110107/day09_03/codes/MFCSerilize2/MFCSerilize2.cpp

    //Download by http://www.NewXing.com
// MFCSerilize2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "afxwin.h"

//继承于CObject
class CAnimal : public CObject
{
public:
    CAnimal( int nLeg = 4 )
    {
        m_nLeg = nLeg;
    }

    void Print( )
    {
        printf( "%p: Leg=%d\n", this, m_nLeg );
    }

    //序列化定义宏
    DECLARE_SERIAL( CAnimal )
    //_DECLARE_DYNCREATE( CAnimal )
	//AFX_API friend CArchive& AFXAPI 
    //  operator>>(CArchive& ar, CAnimal * &pOb);

public:
    //序列化函数
    virtual void Serialize( CArchive& ar );
public:
    int m_nLeg;
    int m_nHand;
    int m_nEye;
};
//序列化实现宏
IMPLEMENT_SERIAL( CAnimal, CObject, VERSIONABLE_SCHEMA|2 )
/*
CObject* PASCAL CAnimal::CreateObject()
{ 
    return new CAnimal; 
} 

_IMPLEMENT_RUNTIMECLASS( CAnimal, CObject, 0, 
		CAnimal::CreateObject )

AFX_CLASSINIT _init_CAnimal( RUNTIME_CLASS(CAnimal) ); 

CArchive& AFXAPI operator>>(CArchive& ar, CAnimal* &pOb)
{ 
    pOb = (CAnimal*) 
        ar.ReadObject(RUNTIME_CLASS(CAnimal)); 
	return ar; 
}
*/

void CAnimal::Serialize( CArchive& ar )
{   //调用父类的序列化函数
    CObject::Serialize( ar );
    //数据的读写
    UINT nSchema =  ar.GetObjectSchema( );
    /*if( 1 == nSchema )
    {
        if( ar.IsLoading( ) )
        {
            ar >> m_nLeg;
            ar >> m_nHand;
        }
        else
        {
            ar << m_nLeg;
            ar << m_nHand;
       }
    }
    else if( nSchema == 2 )*/
    {
        if( ar.IsLoading( ) )
        {
            ar >> m_nLeg;
            ar >> m_nHand;
            ar >> m_nEye;
        }
        else
        {
            ar << m_nLeg;
            ar << m_nHand;
            ar << m_nEye;
        }
    }
}

void ObjectStore( )
{
    //创建文件
    CFile file;
    file.Open( "C:\\obj.dat", 
        CFile::modeCreate|CFile::modeWrite );
    //定义存储的AR
    CArchive ar( &file, CArchive::store );

    //存储对象内的数据
    //CAnimal animal( 10 );
    //animal.Serialize( ar );
    
    //存储对象
    CAnimal * pAnimal2 = new CAnimal( 11 );
    ar << pAnimal2;

    //关闭Ar
    ar.Close( );
    //关闭文件
    file.Close( );
}

void ObjectLoad( )
{
    CFile file;
    file.Open( "C:\\obj.dat", CFile::modeRead );
    CArchive ar( &file, CArchive::load );

    //读取对象内数据
    //CAnimal animal;
    //animal.Serialize( ar );
    //animal.Print( );

    //读取对象
    CAnimal * pAnimal = NULL;
    ar >> pAnimal;
    pAnimal->Print( );

    //关闭
    ar.Close( );
    file.Close( );
}

void ArrayStore( )
{
    CObArray arAnimal;
    for( int nIndex=0; nIndex<50; nIndex++ )
    {
        arAnimal.Add( new CAnimal( nIndex ) );
    }

    CFile file;
    file.Open( "C:\\arobj.dat", 
        CFile::modeCreate|CFile::modeWrite );

    CArchive ar( &file, CArchive::store );

    ar << &arAnimal;

    ar.Close( );
    file.Close( );
}

void ArrayLoad( )
{
    CFile file;
    file.Open( "C:\\arobj.dat", CFile::modeRead );
    CArchive ar( &file, CArchive::load );

    CObArray * parAnimal = NULL;
    ar >> parAnimal;

    ar.Close( );
    file.Close( );

    int nSize = parAnimal->GetSize();
    printf( "Length: %d\n", nSize );
    for( int nIndex=0; nIndex<50; nIndex++ )
    {
        CObject * pObject = 
            parAnimal->GetAt( nIndex );
        ((CAnimal *)pObject)->Print();
    }
}

int main(int argc, char* argv[])
{
    //ObjectStore( );
    //ObjectLoad( );
    ArrayStore( );
    ArrayLoad( );

	return 0;
}